﻿function LanguageViewModel(parent) {
    var self = this;

    // knockout variables
    self.languages = ko.observableArray([]);
    self.workingLanguage = ko.observable(null);
    
    //PAGING + SEARCH
    self.search = ko.observable("");
    self.currentPage = ko.observable(0);
    self.pageSize = ko.observable(10);
    self.pagesCount = ko.observable(1);
    self.count = ko.observable(0);
    self.lastPage = function () {
        self.currentPage(self.pagesCount() - 1);
    };
    self.firstPage = function () {
        self.currentPage(0);
    };
    self.nextPage = function () {
        if (self.currentPage() < self.pagesCount() - 1) {
            self.currentPage(self.currentPage() + 1);
        };
    };
    self.previousPage = function () {
        if (self.currentPage() > 0) {
            self.currentPage(self.currentPage() - 1);
        }
    }; 
    
    //SELECT LANGUAGE
    self.selectLanguage = function (language) {
        self.workingLanguage(language);
    };
    self.deselectLanguage = function () {
        self.workingLanguage(null);
    };
    self.selectWorkingLanguage = function (language) {
        self.selectLanguage(language);
    };
    self.selectNewLanguage = function () {
        var newLanguage = new Language({});
        self.languages.push(newLanguage);
        self.selectLanguage(newLanguage);
    };

    //CRUD OPERATIONS
    self.saveNewLanguage = function () {
        if (self.workingLanguage().errors().length == 0) {
            $.ajax({
                    url: "/Language/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingLanguage()),
                    contentType: 'application/json',
                    success: function(id) {
                        self.workingLanguage().Id(id);
                        self.deselectLanguage();
                        $("#CreateLanguage").modal("hide");
                    }
                });
        }
    };
    self.saveEditLanguage = function () {
        if (self.workingLanguage().errors.length== 0) {
            $.ajax({
                    url: "/Language/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingLanguage()),
                    contentType: 'application/json',
                    success: function() {
                        self.deselectLanguage();
                        $("#EditLanguage").modal("hide");
                    }
                });
        }
    };
    self.deleteLanguage = function () {
        $.post("/Language/DeleteLanguage", { id: self.workingLanguage().Id() }, function () {
            self.languages.pop(self.workingLanguage());
            self.deselectLanguage();
        });
        $('#DeleteLanguage').modal('hide');
    };
    self.cancelNewLanguage = function () {
        self.deselectLanguage();
        self.languages.pop(self.workingLanguage());
        $('#CreateLanguage').modal('hide');
    };
    self.cancelEditLanguage = function () {
        self.loadLanguages();
        self.deselectLanguage();
        $('#EditLanguage').modal('hide');
    };
    self.cancelDetailsLanguage = function () {
        self.deselectLanguage();
        $('#DetailsLanguage').modal('hide');
    };
    self.cancelDeleteLanguage = function () {
        self.deselectLanguage();
        $('#DeleteLanguage').modal('hide');
    };

    self.loadLanguages = function (pageIndex, pageSize, search) {
        $.getJSON("/Language/List", { search: search, pageIndex: pageIndex, pageSize: pageSize }, function (languages) {
            self.pagesCount(languages.PagesCount);
            self.count(languages.Count);
            var mappedLanguages = $.map(languages.Entities, function (item) { return new Language(item); });
            self.languages(mappedLanguages);
        });
    };
    self.triggerLoadlanguages= ko.computed(function () {
        self.currentPage();
        self.pageSize();
        var search = self.search();
        if (parent.route() == "languages") {
            var route = '#/Languages/List/?pageIndex=' + self.currentPage() + '&pageSize=' + self.pageSize();
            if (search != null && search != "") {
                route = route + '&search=' + self.search();
            }
            location.hash = route;
        }
    }, this);

}